#ifndef _dx6i_h
#define _dx6i_h

#include <stdint.h>

#define VERSION "0.7.7"

#define CNT_FREQ		4000UL //Hz
#define CNT_SEC			(CNT_FREQ * 2)
#define CNT_MIN			(CNT_SEC * 60)
#define CNT_MSEC		(CNT_SEC / 1000)

#define BEEP_OUT_INIT	DDRD|= (1 << 4);
#define BEEP_ON			PORTD|= (1 << 4);
#define BEEP_OFF		PORTD&= ~(1 << 4);
#define BEEP_TOGGLE		PIND|= (1 << 4);

#define BEEP_IN_INIT	DDRD&= ~(1 << 2); EICRA|= (1 << ISC00); EIMSK|= (1 << INT0);
#define BEEP_IN_HIGH	(PIND & (1 << 2))

#define LCD_INIT		DDRD|= (1 << 7);
#define LCD_ON			PORTD|= (1 << 7);
#define LCD_OFF			PORTD&= ~(1 << 7);
#define LCD_TOGGLE		PIND|= (1 << 7);
#define PWM_LCD_CNT		(CNT_SEC / 80)

#define VIBRO_INIT		DDRB|= (1 << 1);
#define VIBRO_ON		PORTB|= (1 << 1);
#define VIBRO_OFF		PORTB&= ~(1 << 1);
#define VIBRO_TOGGLE	PINB|= (1 << 1);
#define PWM_VIBRO_CNT	(CNT_SEC / 40)

#define SIZE_OF(x)		(sizeof(x) / sizeof(*(x)))

#define DEBUG_LED_ON	PORTB|= (1 << 5);
#define DEBUG_LED_OFF	PORTB&= ~(1 << 5);
//#define DEBUG

typedef struct _beep_t {
	uint16_t on;
	uint16_t off;
} beep_t;

typedef struct _cnt_t {
	uint8_t rx;
	uint32_t aft;
	uint32_t aft_repeat;
	uint16_t beep;
	uint8_t beeps;
	uint32_t lcd;
	uint16_t vibro;
	uint16_t vibro_detect;
	uint16_t pwm_lcd;
	uint16_t pwm_lcd_val;
	uint16_t pwm_vibro;
	uint16_t pwm_vibro_val;
	uint16_t conf;
} cnt_t;

void dx6i_init();
static void isr_beep(void);
static void isr_lcd(void);
static void isr_vibro(void);
static void isr_aft(void);
void beep_on(uint8_t beeps);
void lcd_on(void);
void vibro_on(bool detect);

#endif
